package config;

import com.baomidou.mybatisplus.generator.config.builder.Controller;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.builder.Mapper;
import com.baomidou.mybatisplus.generator.config.builder.Service;
import config.Strategy.ControllerConfig;
import config.Strategy.EntityConfig;
import config.Strategy.MapperConfig;
import config.Strategy.ServiceConfig;
import lombok.Data;

import java.util.Objects;

/**
 * @Description config
 * @Author zhengzetao
 * @Date 2022/9/16 16:32
 */
@Data
public class StrategyConfig extends Config {
    final private static String GROUP = "StrategyConfig";
    //addInclude(String...)	增加表匹配(内存过滤)	include 与 exclude 只能配置一项
    private String[] include;
    //addExclude(String...)	增加表排除匹配(内存过滤)	include 与 exclude 只能配置一项
    private String[] exclude;
    //addTablePrefix(String...)	增加过滤表前缀
    private String[] tablePrefix;
    //addTableSuffix(String...)	增加过滤表后缀
    private String[] tableSuffix;
    //addFieldPrefix(String...)	增加过滤字段前缀
    private String[] fieldPrefix;
    //addFieldSuffix(String...)	增加过滤字段后缀
    private String[] fieldSuffix;
    //entityBuilder	实体策略配置
    private Entity.Builder entityBuilder;
    //controllerBuilder	controller 策略配置
    private Controller.Builder controllerBuilder;
    //mapperBuilder	mapper 策略配置
    private Mapper.Builder mapperBuilder;
    //serviceBuilder	service 策略配置
    private Service.Builder serviceBuilder;

    private StrategyConfig() {
        super(GROUP);
        this.include = getStrings("include");
        this.exclude = getStrings("exclude");
        if (Objects.nonNull(include) && Objects.nonNull(exclude)) {
            throw new RuntimeException("include 与 exclude 只能配置一项");
        } else if (Objects.isNull(include) && Objects.isNull(exclude)) {
            throw new RuntimeException("请配置需要生成代码的数据库表（include 与 exclude 只能配置一项）");
        }
        this.tablePrefix = getStrings("tablePrefix");
        this.tableSuffix = getStrings("tableSuffix");
        this.fieldPrefix = getStrings("fieldPrefix");
        this.fieldSuffix = getStrings("fieldSuffix");
    }

    public static void setting(com.baomidou.mybatisplus.generator.config.StrategyConfig.Builder builder) {
        StrategyConfig config = new StrategyConfig();
        if (Objects.nonNull(config.include)) {
            builder.addInclude(config.include);
        } else if (Objects.nonNull(config.exclude)) {
            builder.addExclude(config.exclude);
        }
        if (Objects.nonNull(config.tablePrefix)) {
            builder.addTablePrefix(config.tablePrefix);
        }
        if (Objects.nonNull(config.tableSuffix)) {
            builder.addTableSuffix(config.tableSuffix);
        }
        if (Objects.nonNull(config.fieldSuffix)) {
            builder.addFieldSuffix(config.fieldSuffix);
        }
        if (Objects.nonNull(config.fieldPrefix)) {
            builder.addFieldPrefix(config.fieldPrefix);
        }
        com.baomidou.mybatisplus.generator.config.StrategyConfig.Builder strategyConfigBuilder = builder;
        ControllerConfig.setting(builder.controllerBuilder());
        EntityConfig.setting(builder.entityBuilder());
        MapperConfig.setting(builder.mapperBuilder());
        ServiceConfig.setting(builder.serviceBuilder());
    }
}
